package com.huanwei.cc.activity;

import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huanwei.cc.R;
import com.huanwei.cc.base.BaseActivity;
import com.huanwei.cc.bean.MyEvent;
import com.huanwei.cc.utils.SpfUtils;
import com.huanwei.login_regist.LoginActivity;

import de.greenrobot.event.EventBus;

/**
 * Created by ADM on 2017/12/2.
 */

public class Me_set_activity extends BaseActivity implements View.OnClickListener, PopupWindow.OnDismissListener {
    private ImageView me_set_path;
    private LinearLayout me_Agreement, me_Aboutus, me_Clear, me_password_setting, me_Address_management, me_Blacklist;
    private Button me_butten;
    private TextView me_cache_number;
    private int navigationHeight;
    private PopupWindow popupWindow;
    private Handler handler = new Handler() {

        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
                case 0:


            }

        }

    };


    @Override
    public int setLayoutId() {
        return R.layout.me_fragment_set;
    }

    @Override
    public void initViewsAndEvents() {
        int resourceId = getResources().getIdentifier("navigation_bar_height", "dimen", "android");
        navigationHeight = getResources().getDimensionPixelSize(resourceId) - 80;
        findId();



    }



    private void findId() {
        me_cache_number = (TextView) findViewById(R.id.me_cache_number);
        me_set_path = (ImageView) findViewById(R.id.me_set_path);
        me_set_path.setOnClickListener(this);
        me_password_setting = (LinearLayout) findViewById(R.id.me_password_setting);
        me_password_setting.setOnClickListener(this);
        me_Address_management = (LinearLayout) findViewById(R.id.me_Address_management);
        me_Address_management.setOnClickListener(this);
        me_Blacklist = (LinearLayout) findViewById(R.id.me_Blacklist);
        me_Blacklist.setOnClickListener(this);
        me_Clear = (LinearLayout) findViewById(R.id.me_Clear);
        me_Clear.setOnClickListener(this);
        me_Aboutus = (LinearLayout) findViewById(R.id.me_Aboutus);
        me_Aboutus.setOnClickListener(this);
        me_Agreement = (LinearLayout) findViewById(R.id.me_Agreement);
        me_Agreement.setOnClickListener(this);
        me_butten = (Button) findViewById(R.id.me_butten);
        me_butten.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.me_set_path:
                finish();
                break;
            case R.id.me_password_setting:
                startActivity(new Intent(getApplicationContext(), MePasswordActivity.class));
                break;
            case R.id.me_Address_management:
                break;
            case R.id.me_Blacklist:
                break;
            case R.id.me_Clear:
                openPopupWindow(view);
                break;
            case R.id.tv_pick_zone:

                popupWindow.dismiss();
                break;
            case R.id.tv_cancel:
                popupWindow.dismiss();
                break;
            case R.id.me_Aboutus:
                break;
            case R.id.me_Agreement:
                break;
            case R.id.me_butten:

                MyEvent myEvent=new MyEvent();
                myEvent.setType("finish");
                EventBus.getDefault().post(myEvent);
                SpfUtils.put(Me_set_activity.this, "islogin", false);
                startActivity(new Intent(this, LoginActivity.class));
                finish();


                break;


        }

    }

    private void setOnPopupViewClick(View view) {
        TextView tv_pick_zone, tv_cancel;
        tv_pick_zone = (TextView) view.findViewById(R.id.tv_pick_zone);
        tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        tv_pick_zone.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
    }

    //设置屏幕背景透明效果
    public void setBackgroundAlpha(float alpha) {
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.alpha = alpha;
        getWindow().setAttributes(lp);
    }

    @Override
    public void onDismiss() {
        setBackgroundAlpha(1);
    }

    private void openPopupWindow(View v) {
        //防止重复按按钮
        if (popupWindow != null && popupWindow.isShowing()) {
            return;
        }
        //设置PopupWindow的View
        View view = LayoutInflater.from(this).inflate(R.layout.me_popupwindow, null);
        popupWindow = new PopupWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        //设置背景,这个没什么效果，不添加会报错
        popupWindow.setBackgroundDrawable(new BitmapDrawable());
        //设置点击弹窗外隐藏自身
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        //设置动画
        popupWindow.setAnimationStyle(R.style.PopupWindow);
        //设置位置
        popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, navigationHeight);
        //设置消失监听
        popupWindow.setOnDismissListener(this);
        //设置PopupWindow的View点击事件
        setOnPopupViewClick(view);
        //设置背景色
        setBackgroundAlpha(0.5f);
    }



}
